package com.atguigu.gmall.payment;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayOpenPublicTemplateMessageIndustryModifyRequest;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayOpenPublicTemplateMessageIndustryModifyResponse;
import com.alipay.api.response.AlipayTradePagePayResponse;

/**
 * 公钥加密叫加密  公钥解密叫认证
 * 私钥解密叫解密  私钥加密叫签名
 */
public class AlipayDemo {
    //连接支付宝公司网关
    public static void main(String[] args) throws Exception {
        /**
         * 实例化客户端
         */
        //1:网关地址
        //2:AppID
        //3:私钥
        //4:发送的数据格式
        //5:数据的编码
        //6:支付宝公司的公钥
        //7:加密方式

        /**
         * 接口名称 8:统一收单下单 并支持接口
         */

        //实例化客户端
        AlipayClient alipayClient = new DefaultAlipayClient(
                "https://openapi.alipaydev.com/gateway.do",
                "2021000118640235",  //APP_ID
                "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCACgwGEKMgpbEFbTYUySECflZTunkcBwc4/+lEizszJnj4aBeKQBAfIQLp/o5oXy/Y9nhuXBsx96/B9nBLxwutgSgsiGCInAZbl4TkTLpmnSStfwH9gnrjBhjT+pLJ2pDY5lTC70U5gZsJVMEYgAXwbnIbkhnm7cFl68fqTAsvHuCjmVrgagUT3LKZ4eTHUUF77QF82fw4re4OupsS/7X9f/j5HhCENhUHJX2d7eZ6rDY2y+L/33EEB9lxUyepwwAxmXUN8i8Y1O9TuKrZ2ZUvOwEYvsawXRrs7i+apC6REBTXlN9CKQRNX1jK0ZRYOS+yYfTSKn0+wiXUfog6gV8hAgMBAAECggEAHZm1A+XuwIptrVJ9MTr6TahFbt/qRcd+LPhExlrl2cJoLw1pe8WptHieQthdtdVjq2X3m5PhmaCoWlAN0Ab5NShp0DnLD2n6skwKJflzEmv74bw+CoySuk9biA4ix1WnLO265MIKH+xNmk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q", //商家的私钥(签名)
                "json",   //发送的数据格式
                "UTF-8", //5:数据的编码
                "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgCy1m8Lq3Y6h9oRDHP8puEWrWAfnER9EocOlRn7VMmDDBZKKfwmhG7UaZ7GLv76ymg271eQHcaoFSRYTR1bU+TPjy54Z5kSalDGFYIcB6Ldp9jDlZgRL6eiteidBwrzzdjc2a2siQmyvtKce+F5f6tDQQgZpbxOfmNtjNM9qCK50w3Y0JF67rawQt509uzSlutNjwFlUuwa+RGmR1xpp5hBMXr4tXlm1F10iLM8EvPaQSCk5sGo8/l0C+JieW0I+eZu7JIdNLeYRyAPm88Ftlx2vMccYgUFB6f0JsfwqLHAyCXX/DjJsOmBNP58UzQpTB1hEs2XOmDGEnYg+7Ok/6wIDAQAB",  //7:支付宝公司的公钥
                "RSA2"); //7:加密方式

        //8:统一收单下单 并支持接口   alipay.trade.page.pay(统一收单下单并支付页面接口)
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        //TODO 异步回调
        //request.setNotifyUrl("");
        //TODO 同步回调
        //request.setReturnUrl("");

        //请求参数
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no","20210817010101004123"); //支付宝交易编号 唯一必选
        bizContent.put("total_amount",10000); //总金额,必选
        bizContent.put("subject", "测试商品手机");//必选
        bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");//注：目前仅支持FAST_INSTANT_TRADE_PAY必选
        //Calendar calendar  = Calendar.getInstance(); 订单绝对超时时间
        //calendar.add(Calendar.HOUR,1);
        //bizContent.put("time_expire", calendar.getTime());//二维码的  绝对时间  16:30  可选
        System.out.println("json:" + bizContent.toJSONString());
        System.out.println("String:" + bizContent.toString());

        request.setBizContent(bizContent.toString());
        //发出此请求
        AlipayTradePagePayResponse alipayTradePagePayResponse = alipayClient.pageExecute(request);

        if (alipayTradePagePayResponse.isSuccess()){
            System.out.println("调用成功");
            System.out.println(alipayTradePagePayResponse.getBody());
        }else {
            System.out.println("调用失败");
        }
    }
}
/**
 网关
 支付微服务
 */